<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>选项卡</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }

      ul,
      ol,
      li {
        list-style: none;
      }

      .box {
        width: 600px;
        height: 400px;
        border: 3px solid pink;
        margin: 50px auto;
        display: flex;
        flex-direction: column;
      }

      .box > ul {
        height: 60px;
        display: flex;
      }

      .box > ul > li {
        flex: 1;
        background-color: skyblue;
        color: #fff;
        font-size: 20px;
        display: flex;
        justify-content: center;
        align-items: center;
        cursor: pointer;
      }

      .box > ul > li.active {
        background-color: orange;
      }

      .box > ol {
        flex: 1;
        position: relative;
      }

      .box > ol > li {
        width: 100%;
        height: 100%;
        background-color: purple;
        position: absolute;
        left: 0;
        top: 0;
        font-size: 110px;

        display: none;
      }

      .box > ol > li.active {
        display: block;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <ul>
        <li class="active">a</li>
        <li>b</li>
        <li>c</li>
        <li>d</li>
      </ul>
      <ol>
        <li class="active">1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
      </ol>
    </div>
    <script>
      // 分别获取 ul 和 ol 下的 li 元素
      var ulLis = document.querySelectorAll('ul>li');
      var olLis = document.querySelectorAll('ol>li');
      // 循环为每个 li 绑定事件
      for (var i = 0; i < ulLis.length; i++) {
        // 设置自定义属性 index
        ulLis[i].setAttribute('index', i);
        ulLis[i].onclick = function () {
          // 循环为每个 li 移除 active 类名
          for (var i = 0; i < ulLis.length; i++) {
            ulLis[i].classList.remove('active');
            olLis[i].classList.remove('active');
          }
          // 为当前点击的元素增加 active 类名
          this.classList.add('active');
          // 获取当前点击的元素的 index 属性值
          var index = this.getAttribute('index');
          // 获取该 index 属性值对应 ol 下的 li 元素，为其增加 active 类名
          olLis[index].classList.add('active');
        };
      }
    </script>
  </body>
</html>
